package com.pzb.web.controller.wechat;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.wechat.domain.WechatAddress;
import com.pzb.wechat.service.IWechatAddressService;

/**
 * 收货地址Controller
 * 
 * @author Mr_Wu
 * @date 2022-01-21
 */
@RestController
@RequestMapping("/address")
public class WechatAddressController extends BaseController
{
    @Autowired
    private IWechatAddressService wechatAddressService;

    /**
     * 查询收货地址列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam String extAppid)
    {
        List<WechatAddress> list =  wechatAddressService.selectWechatAddressList(getOpenid(),extAppid);
        return AjaxResult.success(list);
    }

    /**
     * 默认收货地址id
     */
    @GetMapping("/defaultId")
    public AjaxResult defaultId(@RequestParam String extAppid)
    {
        Long defaultId = wechatAddressService.selectAddressDefaultId(getOpenid(),extAppid);
        AjaxResult result = AjaxResult.success();
        result.put("defaultId",defaultId);
        return result;
    }

    /**
     * 默认收货地址id
     */
    @PostMapping("/setDefault")
    public AjaxResult setDefault(@RequestBody Map param)
    {
        String addressId = param.get("addressId").toString();
        String extAppid = (String) param.get("extAppid");
        //设置全部为非默认
        wechatAddressService.setAddressIsNotDefault(getOpenid(),extAppid);
        //设为默认
        return toAjax(wechatAddressService.setAddressDefault(Long.parseLong(addressId)));
    }


    /**
     * 获取所有省市数据
     */
    @GetMapping("/region")
    public AjaxResult region()
    {
        AjaxResult result = AjaxResult.success();
        String path = "regionData.json";
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            byte[]  bytes= FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
            result.put("data", JSONObject.parse(new String(bytes,"utf-8")));
        } catch (IOException e) {
            e.printStackTrace();
        }


        return result;
    }

    /**
     * 获取收货地址详细信息
     */
    @GetMapping(value = "/detail")
    public AjaxResult getInfo(@RequestParam("addressId") Long addressId)
    {
        WechatAddress address = wechatAddressService.selectWechatAddressById(addressId);
        if(address!=null){
            address.setRegion(JSON.parseArray(address.getProvince()));
        }
        return AjaxResult.success(address);
    }

    /**
     * 新增收货地址
     */

    @PostMapping("/add")
    public AjaxResult add(@RequestBody WechatAddress wechatAddress)
    {
        wechatAddress.setOpenid(getOpenid());
        wechatAddress.setProvince(wechatAddress.getRegion().toString());
        String fullAddress = "";
        for (Object address : wechatAddress.getRegion()) {
            JSONObject a = (JSONObject) JSONObject.toJSON(address);
            fullAddress+= a.getString("label");
        }
        wechatAddress.setFullAddress(fullAddress+wechatAddress.getAddress());
        return toAjax(wechatAddressService.insertWechatAddress(wechatAddress));
    }

    /**
     * 修改收货地址
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody WechatAddress wechatAddress)
    {
        String fullAddress = "";
        for (Object address : wechatAddress.getRegion()) {
            JSONObject a = (JSONObject) JSONObject.toJSON(address);
            fullAddress+= a.getString("label");
        }
        wechatAddress.setFullAddress(fullAddress+wechatAddress.getAddress());
        return toAjax(wechatAddressService.updateWechatAddress(wechatAddress));
    }

    /**
     * 删除收货地址
     */
	@PostMapping("/remove/{addressId}")
    public AjaxResult remove(@PathVariable Long addressId)
    {
        return toAjax(wechatAddressService.deleteWechatAddressById(addressId));
    }
}
